<?php
// +----------------------------------------------------------------------
// | Siteplus [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.brsttech.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Pillar <service@brsttech.com>
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Model;
use util\Util;


class User extends Model{

    protected $pk       = 'id';
    protected $table    = 'sp_system_user';
    protected $autoWriteTimestamp = true;
    protected $pageNumber = 20;

    /**
     * 添加
     * @access public
     * @param array $param
     * @return bool
     */
    public function add_data($param){
        $data = [
            'username'        => $param['username'],
            'group_id'        => $param['group_id'],
            'password'        => password_hash(md5($param['password']),PASSWORD_DEFAULT),
            'about'           => $param['about'],
            'last_login_ip'   => Util::getIp(),
            'last_login_time' => time(),      
            'create_time'     => time(),
            'update_time'     => time(),      
        ];
        return $this->insertGetId($data);
    } 
    /**
     * 修改
     * @access public
     * @param array $param
     * @return bool
     */
    public function edit_data($param){
        $data = [
            'group_id'    => $param['group_id'],
            'about'       => $param['about'],
            'password'    => password_hash(md5($param['password']),PASSWORD_DEFAULT),
            'create_time' => time(),
        ];
        $where = ['id'=>$param['id']];
        return $this->where($where)->update($data);
    }


    /**
     * [删除]
     * @access public
     * @param int id 删除ID
     * @return bool
     */
    public function delete_data($id){
        return $this->destroy($id);
    }

    /**
     * 判断登录用户
     * @access public
     * @return bool
     */
    public function login($param){
        $condition = [
        	'username'=> $param['username'],
        	'locks'	  => 0,
        ];
	    $info = self::get($condition);
        if($info){
            if(!password_verify(md5($param['password']),$info->getAttr('password'))) {
                return FALSE;
            }
            $data = [
            	'last_login_ip'   => Util::getIp(),
                'last_login_time' => time(),
            ];
            $where = [
            	'id' => $info->getAttr('id'),
            ];
            $result = $this->where($where)->update($data);
            if($result){
                return $info;
            }
            return FALSE;
        }
        return FALSE;
    }

    /**
     * 读取对应主键用户信息
     * @access public
     */
    public function find_user_info($id){
        return self::get($id);
    }

    /**
     * 修改我的密码
     * @access public
     */
    public function upDatePasspowrd($param,$id){
        $data = [
            'password' => password_hash(md5($param['password']),PASSWORD_DEFAULT),
            'about'    => $param['about']
        ];
        $where = ['id' => $id];
        return $this->where($where)->update($data);
    } 

    /**
     * 用户列表
     * @access public
     */
    public function select_user_lists(){
        return $this->order('id desc')->paginate($this->pageNumber);
    }

}